#!/bin/sh
#
# Show me the QA tests that did not pass
#
# Copyright (c) 1997-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

# generic initialization
__scriptname=show-me
. ./common.rc

HOST=`hostname`
QA_HOST=
QA_USER=
QA_DIR=

if [ -n "$PCPQA_DIFF" ]
then
    DIFF="$PCPQA_DIFF"
else
    if which gdiff >/dev/null 2>&1
    then
	DIFF=gdiff
    else
	if which meld >/dev/null 2>&1
	then
	    DIFF=meld
	else
	    if which xxdiff >/dev/null 2>&1
	    then
		DIFF=xxdiff
	    else
		DIFF="diff -c"
	    fi
	fi
    fi
fi

tmp=/tmp/$$
trap "cd /; rm -rf $tmp $tmp.*; exit" 0 1 2 3 15
rm -rf $tmp
mkdir $tmp

not_me=false
local=true
keep=false
where=false
while getopts d:h:klu:w c
do
   case $c in
   d)       QA_DIR=$OPTARG
	    not_me=true
	    ;;

   h)       QA_HOST=$OPTARG
	    local=false
	    ;;

   k)	    keep=true
   	    ;;

   l)	    DIFF="diff -c"
	    ;;

   u)       QA_USER=$OPTARG
	    not_me=true
	    ;;

   w)       where=true
	    wdir=`pwd | sed -e "s;.*/;;"`
   	    ;;

   \?)      echo "Usage: show-me [-lw] [-k] [-d dir] [-h host] [-u user] [test ...]"
	    exit 2;;
   esac
done
shift `expr $OPTIND - 1`

if [ -z "$QA_DIR" ]
then
    if [ -z "$QA_HOST" -o "$QA_HOST" = localhost -o "$QA_HOST" = "`hostname`" ]
    then
	if [ -z "$QA_USER" -o "$QA_USER" = "$LOGNAME" ]
	then
	    # local and really me
	    QA_DIR_DEFAULT=`pwd`
	else
	    QA_DIR_DEFAULT=`ssh $QA_USER@$QA_HOST "/bin/ls -d isms/pcp[1-9]*/qa" 2>/dev/null | grep '^isms/' | tail -1`
	fi
    else
	QA_DIR_DEFAULT=`ssh $QA_USER@$QA_HOST "/bin/ls -d isms/pcp[1-9]*/qa" 2>/dev/null | grep '^isms/' | tail -1`
    fi
    if [ "$QA_DIR_DEFAULT" = "" ]
    then
        QA_DIR_DEFAULT=isms/pcp/qa
    fi
    QA_DIR="$QA_DIR_DEFAULT"
fi

if [ $# -eq -0 ]
then
    if $local
    then
	list="`echo *.out.bad \
	       | tr ' ' '\012' \
	       | sed -e 's/\.out\.bad//' \
	       | LC_COLLATE=POSIX sort -n`"
	if [ "$list" = '*' ]
	then
	    echo "No failing tests!"
	    exit
	fi
	set - `echo $list`
    else
	x=`ssh $QA_USER@$QA_HOST echo $QA_DIR/\[0-9]*.out.bad 2>$tmp.err`
	if [ -z "$x" ]
	then
	    if [ -s $tmp.err ]
	    then
		echo "ssh failed?"
		cat $tmp.err
	    else
		echo "No failing tests!"
	    fi
	    exit
	fi
	set - `echo "$x" \
	       | tr ' ' '\012' \
	       | sed -e 's/.*\///' -e 's/.out.bad//' \
	       | LC_COLLATE=POSIX sort -n`
	echo "Failing tests: $*"
    fi
fi

unset ROOT TOOLROOT MAKEFLAGS
__quick=true
__check=false
. ./common

for id
do
    case "$id"
    in
	[0-9])
		id=00$id
		;;
	[0-9][0-9])
		id=0$id
		;;
    esac

    if [ "$QA_HOST" != $HOST ]
    then
	echo "Fetch files from $QA_USER@$QA_HOST:$QA_DIR ..."
	rm -f $tmp/$id.out.bad $tmp/$id.out
	scp $QA_USER@$QA_HOST:$QA_DIR/$id.out.bad $tmp
	scp $QA_USER@$QA_HOST:$QA_DIR/$id.out $tmp
	cd $tmp
    elif $not_me
    then
	if [ -d $QA_DIR ]
	then
	    cd $QA_DIR
	else
	    echo "$id: No directory: $QA_DIR"
	    exit 1
	fi
    fi
    if $where
    then
	$PCP_ECHO_PROG $PCP_ECHO_N "$wdir/$id: ""$PCP_ECHO_C"
    else
	$PCP_ECHO_PROG $PCP_ECHO_N "$id: ""$PCP_ECHO_C"
    fi
    ref=''
    if [ ! -f $id.out ]
    then
	# if not here, may be CI results, not QA Farm results,
	# so try for reference version in well-known place
	#
	if [ -f $HOME/src/pcp/qa/$id.out ]
	then
	    ref=$HOME/src/pcp/qa/
	    $PCP_ECHO_PROG $PCP_ECHO_N  "Warning: using $id.out from $ref ""$PCP_ECHO_C"
	else
	    echo  "Could not find $id.out in `pwd`"
	    cd $here
	    continue
	fi
    else
	# hack for kenj ...
	#
	case `pwd`
	in
	    */Logs/by-vm/*/qa)
	    	badref=`pwd`/
		;;
	esac
    fi
    if [ ! -f $id.out.bad ]
    then
	echo  "Could not find $id.out.bad in `pwd`"
    else
	echo
	if [ "$DIFF" = xxdiff ]
	then
	    $DIFF $ref$id.out $badref$id.out.bad >/dev/null 2>&1
	else
	    $DIFF $ref$id.out $badref$id.out.bad
	fi
    fi
    if $keep
    then
	if [ -f $here/$id.out.bad ]
	then
	    if diff $here/$id.out.bad $id.out.bad >diff.tmp
	    then
		# same
		:
	    else
		echo "Warning: overwriting $id.out.bad ... diffs"
		cat diff.tmp
		rm -f $here/$id.out.bad
		mv $id.out.bad $here
	    fi
	else
	    mv $id.out.bad $here
	fi
    fi
    cd $here
done
